set more off
log using PS_main_v5.log, text replace

use C:\Users\Makoto\Dropbox\PS_data\JPUS_subs_panel_v8, clear

*Drop subsidiaries that exist only in the 2013 version of Orbis.
drop if exist_2013==1

*Keep only subsidiaries with positive profits in the sample.
keep if pretax_prof>0 & pretax_prof!=.

*Create one-digit and two-digit industry codes.
destring ind_code, replace
generate indcode_1d=floor(ind_code/1000)

*Drop subsidiaries for which information on the one-digit industry code is missing.
drop if indcode_1d==.

*Drop subsidiaries in the financial, insurance, and real estate sectors.
generate indcode_2d=floor(ind_code/100)
tabulate industry if (indcode_2d>=64 & indcode_2d<=66) | indcode_2d==68
drop if (indcode_2d>=64 & indcode_2d<=66) | indcode_2d==68

*Log Transformation
generate ln_preprof=ln(pretax_prof)
generate ln_fassets=ln(fixed_assets)
generate ln_tangible=ln(tangible_assets)
generate ln_comp=ln(costs_employees)
generate ln_gdppc=ln(gdppc)
generate ln_pop=ln(pop)

*Drop subsidiaries included in the estimation sample only after the tax reform.
generate exist_before2009=1 if year<=2008 & (ln_preprof!=. & tax!=. & ln_tangible!=. & ln_comp!=. & ln_gdppc!=. & ln_pop!=. & unemp!=. & gdpgr!=. & total_assets!=.)
bysort subid: egen exist_before2009_total=total(exist_before2009)
drop if exist_before2009_total==0
drop exist_before2009 exist_before2009_total

*Drop subsidiaries included in the estimation sample only before the tax reform.
generate exist_after2009=1 if year>=2009 & (ln_preprof!=. & tax!=. & ln_tangible!=. & ln_comp!=. & ln_gdppc!=. & ln_pop!=. & unemp!=. & gdpgr!=. & total_assets!=.)
bysort subid: egen exist_after2009_total=total(exist_after2009)
drop if exist_after2009_total==0
drop exist_after2009 exist_after2009_total

*Retain in the sample only subsidiaries located in the countries where both Japanese- and US-owned subsidiaries exist.
generate sub=0
replace sub=1
bysort country JP: egen sub_country_JP=total(sub)
bysort country: egen sub_country=total(sub)
table country JP if sub_country==sub_country_JP, statistic(total sub)
drop if sub_country==sub_country_JP
drop sub sub_country sub_country_JP

*Distribution of Foreign Subsidiaries across Host Countries
estpost tabulate country JP
esttab . using distribution, replace tex cells("b(fmt(%10.3gc)) colpct(fmt(2) par)") unstack noobs title(Distribution of Foreign Subsidiaries across Host Countries) nonumbers

*Number of Foreign Subsidiaries in Each Year
estpost tabulate year JP
esttab . using count, replace tex cells(b(fmt(%10.3gc))) unstack noobs title(Number of Subsidiaries with Positive Pre-Tax Profits) nonumbers

*Data Check
tabstat pretax_prof tangible_assets costs_employees total_assets intan_assets tax if US==1, by(year) stat(mean n)
tabstat pretax_prof tangible_assets costs_employees total_assets intan_assets tax if JP==1, by(year) stat(mean n)
tabstat pretax_prof tangible_assets costs_employees total_assets intan_assets tax, by(year) stat(mean n)
correlate pretax_prof tangible_assets costs_employees total_assets intan_assets tax

tabulate JP
bysort JP: distinct subid

*Define large subsidiaries in the full sample.
bysort subid: egen mean_assets=mean(total_assets)
bysort subid: generate sub_n=_n if mean_assets!=.
generate large_p50=0 if mean_assets!=.
summarize mean_assets if sub_n==1, detail
replace large_p50=1 if mean_assets>r(p50) & mean_assets!=.
drop mean_assets sub_n
tabulate JP large_p50
bysort JP large_p50: distinct subid
sort subid year

*Label variables.
label variable ln_preprof "Log of Pre-Tax Profit"
label variable tax "Corporate Tax Rate \(\left(Tax_{it}\right)\)"
label variable ln_tangible "Log of Tangible Fixed Assets"
label variable ln_comp "Log of Compensation"
label variable ln_gdppc "Log of GDP per Capita"
label variable ln_pop "Log of Population"
label variable unemp "Unemployment Rate"
label variable gdpgr "GDP Growth Rate"
label variable total_assets "Total Assets"

*Summary Statistics
estpost tabstat ln_preprof tax ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr total_assets, by(JP) statistics(mean sd median n) columns(statistics)
esttab . using PS_sum_stat, replace tex cells("mean(fmt(%10.3gc)) sd(fmt(%10.3gc)) p50(fmt(%10.3gc)) count(fmt(%10.3gc))") noobs label substitute(\_ _) title(Summary Statistics) nonumbers

*Preliminary Analysis
label variable tax "\$ Tax_{it}$"

*Tax Sensitivity of Pre-Tax Profits
local varlist_base1 "tax ln_tangible ln_comp"
local varlist_base2 "tax ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr"

xtreg ln_preprof `varlist_base1' i.year#i.indcode_1d if US==1, fe vce(cluster subid)
outreg2 using PS_baseline, replace dec(4) nocons keep(`varlist_base2') label title("Tax Sensitivity of Pre-Tax Profits") addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if US==1, fe vce(cluster subid)
outreg2 using PS_baseline, dec(4) nocons keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base1' i.year#i.indcode_1d if JP==1, fe vce(cluster subid)
outreg2 using PS_baseline, dec(4) nocons keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if JP==1, fe vce(cluster subid)
outreg2 using PS_baseline, dec(4) nocons tex excel keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

*Heterogeneous Tax Sensitivity Depending on Firm Size
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if US==1 & large_p50==0, fe vce(cluster subid)
outreg2 using PS_hetero, replace dec(4) nocons keep(`varlist_base2') label title("Heterogeneous Tax Sensitivity Depending on Firm Size") addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if US==1 & large_p50==1, fe vce(cluster subid)
outreg2 using PS_hetero, dec(4) nocons keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if JP==1 & large_p50==0, fe vce(cluster subid)
outreg2 using PS_hetero, dec(4) nocons keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)
xtreg ln_preprof `varlist_base2' i.year#i.indcode_1d if JP==1 & large_p50==1, fe vce(cluster subid)
outreg2 using PS_hetero, dec(4) nocons tex excel keep(`varlist_base2') label addtext(Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

*Main Analysis
generate US_tax=US*tax
label variable US_tax "\$ US_i \times Tax_{it}$"
generate JP_tax=JP*tax
label variable JP_tax "\$ JP_i \times Tax_{it}$"

forvalues i = 2004/2016 {
generate year`i'=0
replace year`i'=1 if year==`i'
}

forvalues i = 2004/2016 {
generate tax_year`i'=tax*year`i'
label variable tax_year`i' "\$ Tax_{it} \times Year \ `i'$"
}

foreach x in US JP {

forvalues i = 2004/2016 {
generate `x'_tax_year`i'=`x'*tax*year`i'
label variable `x'_tax_year`i' "\$ `x'_i \times Tax_{it} \times Year \ `i'$"
}

}

*Compare the tax semi-elasticities between US- and Japanese-owned foreign subsidiaries for each year from 2004 to 2016.
foreach x in US JP {

forvalues i = 2004/2016 {
generate minus_`x'_tax_year`i'=-`x'*tax*year`i'
}

}

forvalues i = 2004/2016 {
generate plot_coef_`i'=-US*tax*year`i'
}

xtreg ln_preprof plot_coef_* minus_JP_tax_year* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
estimates store US_tax_semielasticity
drop plot_coef_*

forvalues i = 2004/2016 {
generate plot_coef_`i'=-JP*tax*year`i'
}

xtreg ln_preprof minus_US_tax_year* plot_coef_* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
estimates store JP_tax_semielasticity
drop plot_coef_*

coefplot (US_tax_semielasticity, msymbol(square) pstyle(p2)) (JP_tax_semielasticity, msymbol(circle) pstyle(p1)), keep(plot_coef_*) vertical levels(90) nolabels rename(plot_coef_*="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(4 5 "Announcement", placement(w) size(small)) text(4 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Tax semi-elasticity", size(small)) plotlabels("US-Owned Subsidiaries" "Japanese-Owned Subsidiaries") legend(size(small)) graphregion(color(white)) saving(JPUS_tax_semielasticity_figure, replace)
graph export JPUS_tax_semielasticity_figure.pdf, replace as(pdf)

xtreg ln_preprof US_tax_year* JP_tax_year* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
outreg2 using JPUS_tax_semielasticity, replace dec(4) nocons tex excel keep(US_tax_year* JP_tax_year*) label title("Tax Semi-Elasticity for US- and Japanese-Owned Subsidiaries from 2004 to 2016") addtext(Home Country-Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

*Test the statistical significance of the difference in the tax semi-elasticities between Japanese- and US-owned foreign subsidiaries for each year from 2004 to 2016.
forvalues i = 2004(1)2016 {
*Two-Sided Test
lincom _b[JP_tax_year`i']-_b[US_tax_year`i']

*One-Sided Test
display "H_0: difference_JPUS>=0 in `i' p-value =" t(r(df), r(t))
}

generate Small_JP_tax=(1-large_p50)*JP*tax
label variable Small_JP_tax "\$ Small_i \times JP_i \times Tax_{it}$"
generate Large_JP_tax=large_p50*JP*tax
label variable Large_JP_tax "\$ Large_i \times JP_i \times Tax_{it}$"

foreach x in Small Large {

forvalues i = 2004/2016 {
generate `x'_JP_tax_year`i'=`x'_JP_tax*year`i'
label variable `x'_JP_tax_year`i' "\$ `x'_i \times JP_i \times Tax_{it} \times Year \ `i'$"
}

}

*Compare the tax semi-elasticities between US-owned subsidiaries and large/small Japanese-owned subsidiaries for each year from 2004 to 2016.
foreach x in Small Large {

forvalues i = 2004/2016 {
generate minus_`x'_JP_tax_year`i'=-`x'_JP_tax*year`i'
}

}

forvalues i = 2004/2016 {
generate plot_coef_`i'=-US*tax*year`i'
}

xtreg ln_preprof plot_coef_* minus_Small_JP_tax_year* minus_Large_JP_tax_year* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
estimates store US_tax_semielasticity
drop plot_coef_*

forvalues i = 2004/2016 {
generate plot_coef_`i'=-Small_JP_tax*year`i'
}

xtreg ln_preprof minus_US_tax_year* plot_coef_* minus_Large_JP_tax_year* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
estimates store Small_JP_tax_semielasticity
drop plot_coef_*

forvalues i = 2004/2016 {
generate plot_coef_`i'=-Large_JP_tax*year`i'
}

xtreg ln_preprof minus_US_tax_year* minus_Small_JP_tax_year* plot_coef_* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
estimates store Large_JP_tax_semielasticity
drop plot_coef_* minus_US_tax_year* minus_JP_tax_year* minus_Small_JP_tax_year* minus_Large_JP_tax_year*

coefplot (US_tax_semielasticity, msymbol(square) pstyle(p2)) (Large_JP_tax_semielasticity, msymbol(circle) pstyle(p1)), keep(plot_coef_*) vertical levels(90) nolabels rename(plot_coef_*="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(6 5 "Announcement", placement(w) size(small)) text(6 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Tax semi-elasticity", size(small)) plotlabels("US-Owned Subsidiaries" "Large Japanese-Owned Subsidiaries") legend(size(small)) graphregion(color(white)) saving(large_JP_tax_semielasticity_figure, replace)
graph export large_JP_tax_semielasticity_figure.pdf, replace as(pdf)

coefplot (US_tax_semielasticity, msymbol(square) pstyle(p2)) (Small_JP_tax_semielasticity, msymbol(circle) pstyle(p1)), keep(plot_coef_*) vertical levels(90) nolabels rename(plot_coef_*="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(4 5 "Announcement", placement(w) size(small)) text(4 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Tax semi-elasticity", size(small)) plotlabels("US-Owned Subsidiaries" "Small Japanese-Owned Subsidiaries") legend(size(small)) graphregion(color(white)) saving(small_JP_tax_semielasticity_figure, replace)
graph export small_JP_tax_semielasticity_figure.pdf, replace as(pdf)

xtreg ln_preprof US_tax_year* Small_JP_tax_year* Large_JP_tax_year* ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
outreg2 using large_JP_tax_semielasticity, replace dec(4) nocons tex excel keep(US_tax_year* Small_JP_tax_year* Large_JP_tax_year*) label title("Tax Semi-Elasticity for US-Owned and Large Japanese-Owned Subsidiaries from 2004 to 2016") addtext(Subsidiary Size-Home Country-Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

*Test the statistical significance of the difference in the tax semi-elasticities between large Japanese-owned subsidiaries and US-owned subsidiaries for each year from 2004 to 2016.
forvalues i = 2004(1)2016 {
*Two-Sided Test
lincom _b[Large_JP_tax_year`i']-_b[US_tax_year`i']

*One-Sided Test
display "H_0: difference_JPUS>=0 in `i' p-value =" t(r(df), r(t))
}

*Test the statistical significance of the difference in the tax semi-elasticities between small Japanese-owned subsidiaries and US-owned subsidiaries for each year from 2004 to 2016.
forvalues i = 2004(1)2016 {
*Two-Sided Test
lincom _b[Small_JP_tax_year`i']-_b[US_tax_year`i']

*One-Sided Test
display "H_0: difference_JPUS>=0 in `i' p-value =" t(r(df), r(t))
}

*Estimate the difference in the tax semi-elasticities between Japanese- and US-owned subsidiaries for each year from 2004 to 2016 (Base Year: 2007).
xtreg ln_preprof tax JP_tax ib2007.year#c.JP_tax ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
xtreg, coeflegend

coefplot, keep(*.year#c.JP_tax) vertical omitted baselevels levels(90) nolabels rename(*.year#c.JP_tax="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(2 5 "Announcement", placement(w) size(small)) text(2 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Difference in the coefficients on the host-country tax rate" "between Japanese- and US-owned subsidiaries", size(small)) graphregion(color(white)) saving(JP_subs_response_figure, replace)
graph export JP_subs_response_figure.pdf, replace as(pdf)

xtreg ln_preprof tax JP_tax JP_tax_year2004-JP_tax_year2006 JP_tax_year2008-JP_tax_year2016 ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
outreg2 using JP_subs_response, replace dec(4) nocons tex excel keep(tax JP_tax JP_tax_year2004-JP_tax_year2006 JP_tax_year2008-JP_tax_year2016) label title("Difference in the Tax Semi-Elasticities between Japanese- and US-Owned Subsidiaries (Base Year: 2007)") addtext(Home Country-Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

*Estimate the difference in the tax semi-elasticities between large/small Japanese-owned subsidiaries and US-owned subsidiaries for each year from 2004 to 2016 (Base Year: 2007).
xtreg ln_preprof tax Small_JP_tax Large_JP_tax ib2007.year#c.Small_JP_tax ib2007.year#c.Large_JP_tax ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
xtreg, coeflegend

coefplot, keep(*.year#c.Large_JP_tax) vertical omitted baselevels levels(90) nolabels rename(*.year#c.Large_JP_tax="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(2 5 "Announcement", placement(w) size(small)) text(2 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Difference in the coefficients on the host-country tax rate" "between large Japanese- and US-owned subsidiaries", size(small)) graphregion(color(white)) saving(large_JP_subs_response_figure, replace)
graph export large_JP_subs_response_figure.pdf, replace as(pdf)

coefplot, keep(*.year#c.Small_JP_tax) vertical omitted baselevels levels(90) nolabels rename(*.year#c.Small_JP_tax="") ciopts(recast(rcap)) xline(5 6, lpattern(dot) lcolor(black)) text(4 5 "Announcement", placement(w) size(small)) text(4 6 "Enactment", placement(e) size(small)) xtitle("Year", size(small)) xlabel(, labsize(small)) ytitle("Difference in the coefficients on the host-country tax rate" "between small Japanese- and US-owned subsidiaries", size(small)) graphregion(color(white)) saving(small_JP_subs_response_figure, replace)
graph export small_JP_subs_response_figure.pdf, replace as(pdf)

xtreg ln_preprof tax Small_JP_tax Large_JP_tax Small_JP_tax_year2004-Small_JP_tax_year2006 Small_JP_tax_year2008-Small_JP_tax_year2016 Large_JP_tax_year2004-Large_JP_tax_year2006 Large_JP_tax_year2008-Large_JP_tax_year2016 ln_tangible ln_comp ln_gdppc ln_pop unemp gdpgr i.large_p50#i.JP#i.indcode_1d#i.year, fe vce(cluster subid)
outreg2 using large_JP_subs_response, replace dec(4) nocons tex excel keep(tax Small_JP_tax Large_JP_tax Small_JP_tax_year2004-Small_JP_tax_year2006 Small_JP_tax_year2008-Small_JP_tax_year2016 Large_JP_tax_year2004-Large_JP_tax_year2006 Large_JP_tax_year2008-Large_JP_tax_year2016) label title("Difference in the Tax Semi-Elasticities between Large Japanese-Owned and US-Owned Subsidiaries (Base Year: 2007)") addtext(Subsidiary Size-Home Country-Industry-Year Fixed Effects, Yes, Subsidiary Fixed Effects, Yes)

log close